const app = getApp();
import { receiveCoupon } from '../../../utils/requestApi.js'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    goodsInfo: Object,
  },

  /**
   * 组件的初始数据
   */

  data: {
    imgUrl: app.data.imgurl,
    hiddenModal: true,  //显示弹框  选择优惠券
  },

  ready: function () {
    
  },

  /**
   * 组件的方法列表
   */
  methods: {

    //打开 / 关闭  modal
    showModal:function(){
      if (this.data.goodsInfo.Coupons.length == 0){
        wx.showToast({
          icon: 'none',
          title: '暂无优惠券',
        })
        return
      }
      this.setData({
        hiddenModal: !this.data.hiddenModal
      })
    },

    //领取优惠券
    selectCou: function (e) {
      let { goodsInfo } = this.data;
      receiveCoupon({ couponId: e.currentTarget.dataset.id})
      .then(res =>{
        if (res.data.Result == "Success"){
          goodsInfo.Coupons.forEach(item => {
            if (item.CouponId == e.currentTarget.dataset.id){
              item.IsCurCouponUserGet = 1;
              this.setData({ goodsInfo })
            }
          })
        }
      })
    },
  }
})
